home *** CD-ROM | disk | FTP | other *** search
/ CD/PC Actual 76 / DVD Actual 1 Marzo 2003.iso / Trial / TurboCAD 7.1 Pro / Data.Cab / F25228_textDisplay.java < prev    next >
Encoding:
Java Source  |  2000-11-10  |  13.3 KB  |  351 lines

  1. /**
  2. /*
  3.     Rob Mayfield. IMSI @1997
  4.     The text window uses a small subset of HTML to display multiple color
  5.     in the text. This is done in the displayText class.
  6.     Eventually Multi font, size, style and color text box
  7. **/
  8. import java.awt.*;
  9.  
  10. public class textDisplay extends Canvas
  11.     String Text;
  12.     int width, height, left, top; // area of text box
  13.     java.awt.Color foreGround, backGround;
  14.     java.util.Vector dText = new java.util.Vector();
  15.     java.util.Vector dColor = new java.util.Vector();
  16.     int curPos = 0;
  17.     int viewLeftPos = 0;
  18.     int topLine = 0;   // first line on screen
  19.     int prevTopLine = -1;  // previous top line on screen. Used in checking if repaint needed.
  20.     int aveCharWidth = 0;
  21.     int textLengthMax = 0, textCharLenghtMax = 0;
  22.     int totalLines = 0;
  23.     int lineHeight = 0;
  24.     int lines = 0;
  25.     boolean first = true;
  26.     ViewSourceDialog parent;
  27.     //Color prevTopColor = Color.black;
  28.     Font topFont;
  29.     
  30.     int wow=0;
  31.  
  32.     textDisplay ()
  33.     {   super();
  34.         foreGround = Color.black;
  35.         backGround = Color.white;
  36.         this.parent = (ViewSourceDialog) getParent();
  37.     }
  38.     
  39.     public void setText(String Str)
  40.     {
  41.         int jj=0;
  42.         Text = Str;
  43.         String S;
  44.         Color prevColor = foreGround;  // set up first color for line 1
  45.         int cnt = 0;
  46.         while ((S = getNextString()) != null) 
  47.         {
  48.             String trimStr = rTrim(S);
  49.             dText.addElement(trimStr);  // add to Vector
  50.  
  51.             boolean tag = false;
  52.             int cntChar = 0;
  53.             for (int ii = 0; ii< trimStr.length(); ii++)
  54.             {   if (trimStr.charAt(ii) == '<') tag = true;
  55.                 if (trimStr.charAt(ii) == '>') tag = false;
  56.                 if (!tag) cntChar++;
  57.             }
  58.             if (cntChar > textCharLenghtMax) 
  59.                 textCharLenghtMax = cntChar;
  60.  
  61.             Color c;
  62.             if (cnt == 0) 
  63.             {   // find start color for first line
  64.                 String upStr = trimStr.toUpperCase();
  65.                 int fPos, cPos;
  66.                 if ((fPos=upStr.indexOf("<FONT")) > -1 && (cPos=upStr.indexOf("COLOR=")) > -1)
  67.                 {   if (fPos != 0)  // font not set in first chars
  68.                         prevColor = foreGround;
  69.                     else
  70.                         prevColor = getFirstColor (trimStr);
  71.                 }
  72.                 else
  73.                     prevColor = foreGround;
  74.                 dColor.addElement(prevColor);  // add the color to start line 0
  75.             }
  76.             else
  77.             {
  78.                 dColor.addElement(prevColor);
  79.                 prevColor = ((c=getEndColor (trimStr)) == null) ? prevColor : c;
  80.             }
  81.             //System.out.println("cnt: " + cnt
  82.             cnt++;
  83.         }
  84.         
  85.         repaint();
  86.     }
  87.  
  88.     // finds the next string delimited by HTML <br>
  89.     public String getNextString()
  90.     {   int Start = curPos;
  91.         int End = curPos;
  92.         boolean found = false;
  93.         if (Start > Text.length() || Text.length() == 0) return null;
  94.         while (curPos < Text.length())
  95.         {   
  96.             char c = Text.charAt(curPos);
  97.             if (curPos+3 < Text.length() && Text.charAt(curPos) == '<' && (Text.charAt(curPos+1) == 'b' ||
  98.                 Text.charAt(curPos+1) == 'B') && (Text.charAt(curPos+2) == 'r' || Text.charAt(curPos+2) == 'R') 
  99.                 && Text.charAt(curPos+3) == '>')
  100.             {   End = curPos;
  101.                 curPos += 4;
  102.                 found = true;
  103.                 break;
  104.             }
  105.             curPos++;
  106.             //c = 'a';
  107.         }
  108.         
  109.         if (curPos == Text.length())
  110.         {   if (End == Start) 
  111.             {   curPos++; // increment so next call to getNextString() will return null
  112.                 return Text.substring(Start, curPos-1);
  113.             }
  114.             else return Text.substring(Start, End);
  115.         }
  116.         else if (End == Start)
  117.         {   if (found) return (" "); // return blank line (well almost blank with one space). we do this so calling routine reads another line.
  118.             return null;
  119.         }
  120.         else
  121.         {   
  122.             return (Text.substring(Start, End));
  123.         }
  124.     }
  125.         
  126.     public void resize (int width, int height)
  127.     {   this.width = width;
  128.         this.height = height;
  129.         repaint();
  130.     }
  131.  
  132.     public synchronized void  reshape (int left, int top, int width, int height)
  133.     {   this.left = left;
  134.         this.top = top;
  135.         this.width = width;
  136.         this.height = height;
  137.         super.reshape(left, top, width, height);
  138.         lines = 0;  // reset the number of showing lines in text area
  139.  
  140.         repaint();
  141.     }
  142.  
  143.     public synchronized void setBackground(Color c)
  144.     {
  145.         super.setBackground(c);
  146.         backGround = c;
  147.  
  148.     }
  149.  
  150.     public synchronized void setForeground(Color c)
  151.     {
  152.         super.setForeground(c);
  153.         foreGround = c;
  154.     }
  155.  
  156.     public void verticalScrollbarAction(int Action, Object arg)
  157.     {
  158.         int sPos = ((Integer) arg).intValue();
  159.         topLine = sPos;
  160.         if (topLine != prevTopLine) 
  161.             repaint();
  162.         prevTopLine = topLine;
  163.     }
  164.  
  165.     public void horizontalScrollbarAction(int Action, Object arg)
  166.     {
  167.         int sPos = ((Integer) arg).intValue();
  168.         viewLeftPos = -sPos * aveCharWidth;
  169.         repaint();
  170.     }
  171.  
  172.     // get color for end of line by reading FONT tag
  173.     public Color getEndColor (String Str)
  174.     {
  175.         int fPos, cPos, pos = 0;
  176.         String upStr = Str.toUpperCase();      
  177.  
  178.         if ((fPos=upStr.lastIndexOf("<FONT")) > -1 && (cPos=upStr.lastIndexOf("COLOR=")) > -1)
  179.         {    
  180.             cPos += 6;    
  181.             if (Str.charAt(cPos)=='\"') cPos++;  // find beginning of hex or long color number
  182.             if (Str.charAt(cPos)=='\'') cPos++;  // find beginning of hex or long color number
  183.             if (Str.charAt(cPos)=='#') 
  184.             {    cPos++;  // find beginning of hex or long color number
  185.     
  186.                 int Start = cPos; // starting position of number
  187.                 while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
  188.                 String hexString = upStr.substring(Start, cPos); // get number as string
  189.                 while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number                
  190.                 if (hexString.length() != 6) return null;                 
  191.                 int intColor = Integer.parseInt(hexString, 16);  // convert from hex string to int
  192.                 return (new Color(intColor));  // new color                
  193.             }
  194.             else
  195.                 return null;
  196.         }
  197.         else
  198.             return null;
  199.     }
  200.  
  201.     // get color for beginning of each line
  202.     public Color getFirstColor (String Str)
  203.     {
  204.         int fPos, cPos, pos = 0;
  205.         String upStr = Str.toUpperCase();      
  206.  
  207.         if ((fPos=upStr.indexOf("<FONT", pos)) > -1 && (cPos=upStr.indexOf("COLOR=", pos)) > -1)
  208.         {   
  209.             cPos += 6;    
  210.             if (Str.charAt(cPos)=='\"') cPos++;  // find beginning of hex or long color number
  211.             if (Str.charAt(cPos)=='\'') cPos++;  // find beginning of hex or long color number
  212.             if (Str.charAt(cPos)=='#') 
  213.             {    cPos++;  // find beginning of hex or long color number
  214.     
  215.                 int Start = cPos; // starting position of number
  216.                 while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
  217.                 String hexString = upStr.substring(Start, cPos); // get number as string
  218.                 while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number                
  219.                 if (hexString.length() != 6) return null;                 
  220.                 int intColor = Integer.parseInt(hexString, 16);  // convert from hex string to int
  221.                 return (new Color(intColor));  // new color                
  222.             }
  223.             else
  224.                 return null;
  225.         }
  226.         else
  227.             return null;
  228.  
  229.     }
  230.  
  231.     public void paint(java.awt.Graphics g)
  232.     {
  233.         String Str, resultStr, upStr, drawStr;
  234.  
  235.         Font cFont = new Font("Courier", Font.BOLD, 12);
  236.         g.setFont(cFont);
  237.         //g.setColor(prevTopColor);  // get default color
  238.         Color fg = foreGround;
  239.  
  240.         FontMetrics fm = g.getFontMetrics(cFont);
  241.  
  242.         if (aveCharWidth == 0) aveCharWidth = fm.stringWidth("M"); // get the average char width for incremental scrolling
  243.         if (lineHeight == 0) lineHeight = fm.getHeight(); // get the height of a line
  244.       
  245.         if (lines == 0) lines = height/lineHeight;
  246.         int num = ((dText.size() - topLine) < lines) ? dText.size() - topLine : lines;
  247.  
  248.         // cycle through the lines of text and draw to screen
  249.         for (int ii = 0; ii < num; ii++)
  250.         {
  251.  
  252.             if (topLine > dText.size() - 1) break;
  253.             Str = (String)dText.elementAt(ii+topLine);
  254.             g.setColor((Color)dColor.elementAt(ii+topLine));
  255.             //g.setFont((Font)dFont.elementAt(ii+topLine));
  256.             int drawPos = 1;        // starting position to draw string
  257.             upStr = Str.toUpperCase();      
  258.  
  259.             int cPos = -1;
  260.             int fPos = -1;         
  261.             int pos = 0; 
  262.             boolean printAsIs = false;
  263.             boolean foundFontTag = false;
  264.             // see if font or color change. If not just draw text for line, else draw in different font and color
  265.             while ((fPos=upStr.indexOf("<FONT", pos)) > -1 && (cPos=upStr.indexOf("COLOR=", pos)) > -1)
  266.             {   foundFontTag = true;
  267.                 printAsIs = false;                
  268.                 cPos += 6;    
  269.                 if (Str.charAt(cPos)=='\"') cPos++;  // find beginning of hex or long color number
  270.                 if (Str.charAt(cPos)=='\'') cPos++;  // find beginning of hex or long color number
  271.                 if (Str.charAt(cPos)=='#') cPos++;  // find beginning of hex or long color number
  272.                 else 
  273.                 {   printAsIs = true;   
  274.                     break; // not a valid hex number ("#FF00FF" format)
  275.                 }
  276.                 int Start = cPos; // starting position of number
  277.                 while (Str.charAt(cPos) != '\"' && Str.charAt(cPos) != ' ' && Str.charAt(cPos) != '\'') cPos++; // find end of number
  278.                 String hexString = upStr.substring(Start, cPos); // get number as string
  279.                 while (Str.charAt(cPos) != '>' && cPos < Str.length()) cPos++; // find end of number                
  280.                 if (hexString.length() != 6) 
  281.                 {   printAsIs = true;   
  282.                     break; // not a valid hex number ("#FF00FF" format)
  283.                 }
  284.  
  285.                 drawStr = Str.substring(pos, fPos);
  286.                 g.drawString(drawStr, drawPos+viewLeftPos, lineHeight * (ii+1));  // draw string in current color
  287.  
  288.                 drawPos += fm.stringWidth(drawStr);
  289.                 int intColor = Integer.parseInt(hexString, 16);  // convert from hex string to int
  290.                 fg = new Color(intColor);  // set new color                
  291.                 g.setColor(fg);  // return to entry color                
  292.                 pos = cPos+1;
  293.             }
  294.             if (foundFontTag)
  295.             {   g.setColor(fg);  // return to entry color                              
  296.                 g.drawString(Str.substring(pos, Str.length()), drawPos+viewLeftPos, lineHeight * (ii+1));  // draw string in current color                
  297.                 if (first)  // find max length of string
  298.                 {   int wid = drawPos + fm.stringWidth(Str.substring(pos, Str.length()));  // add last segment to length
  299.                     if (wid > textLengthMax) 
  300.                         textLengthMax = wid;
  301.                 }
  302.  
  303.             }
  304.             if (!foundFontTag || printAsIs)
  305.             {   if (first)  // find max length of string
  306.                 {   int wid = fm.stringWidth(Str);
  307.                     if (wid > textLengthMax) 
  308.                         textLengthMax = wid;
  309.                 }
  310.                 g.drawString (Str, 1+viewLeftPos, lineHeight * (ii+1));  
  311.             }
  312.  
  313.             foreGround = fg;  // return to entry color
  314.             //if (ii == 0) prevTopColor = fg;
  315.         }
  316.  
  317.         if (first)
  318.         {
  319.  
  320.             textLengthMax = (textLengthMax + 30) / aveCharWidth;  // add slop space for scrolling
  321.             if (textCharLenghtMax > textLengthMax) textLengthMax = textCharLenghtMax;
  322.             ViewSourceDialog parent = (ViewSourceDialog) getParent();
  323.             parent.horizontalScrollbar.setPageIncrement(textLengthMax/4);
  324.             parent.horizontalScrollbar.setValues(0, 6, 0, textLengthMax);
  325.  
  326.             totalLines = dText.size();
  327.  
  328.             parent.verticalScrollbar.setPageIncrement(totalLines/4);
  329.             parent.verticalScrollbar.setValues(0, 6, 0, totalLines);
  330.  
  331.         }
  332.         first = false;
  333.     }
  334.  
  335.     public String rTrim(String Str) 
  336.     {   int iLen, len;
  337.         len = iLen = Str.length() - 1;
  338.         int st = 0;
  339.         while ((len > 0) && (Str.charAt(len) <= ' ')) 
  340.         {
  341.             len--;
  342.         }
  343.         return (len < iLen) ? Str.substring(st, len+1) : Str;
  344.     }
  345.  
  346.  
  347. }   // End of class textDisplay
  348.  
  349.  
  350.